const { createBiTree,createBitreeByLevelOrder } =  require("./createBitree");

const biTree = createBiTree(10);


console.log(biTree);

/**
 * 
 * @param {biTree} root 
 * @description 二叉树前序遍历 递归实现
 * @returns 
 */
const preOrderByRecursion = function (root) {
    if(root === null) {
        return;
    }
    console.log(root.value);
    preOrderByRecursion(root.left);
    preOrderByRecursion(root.right);

}

console.log('preOrderByRecursion------------------');
preOrderByRecursion(biTree);


/**
 * 
 * @description 借助栈实现非递归前序遍历二叉树
 */
const preOrderByNoRecursion = function(root) {

    const stack = [];
    
    let temp = root;

    while(stack.length !== 0 || temp !== null) {
        
        if(temp !== null) {
            
            stack.push(temp);
            console.log(temp.value);
            temp = temp.left;
           
        }else {
            temp = stack.pop();
            temp = temp.right;
            
        }

    }
}

console.log('preOrderByNoRecursion------------------');
preOrderByNoRecursion(biTree);



